
//////////////////////// Page Initializing ////////////
//
jQuery(document).ready(function() {			
	///// SET DEFAULT CSS CLASS FOR IMPROMPTU PLUGIN ///
							
});


//////////////////////// LOADING A PAGE ////////////
//
function loadPackagePage(sourceDiv, actionURL, packageId) {	
		
	$.ajax ({		
		url:	  	actionURL + "?set_ajax_view",
		data:		"package_id=" + packageId,		
		dataType: 	"html",
		beforeSend: function() {						
			$('div#' + sourceDiv).hide("slow");			
		},
		complete: 	function() {
			$('div#' + sourceDiv).show("fast");			
		},
		success:  function(data) {
			$("#" + sourceDiv).html(data);			
		}
	});		
}

//////////////////// PACKAGE SERVICE //////////////


//////////////////// DELETE A PACKAGE ////////////////////////////
//
function deletePackage(packageId){
	promptConfirm(_("Delete"),_('Are you sure to delete this service package?'), function() {
		submitDeletePackage(packageId);
		return true;
	},'YesNo','warn',_('No'));
}

function submitDeletePackage(packageId) {
	$.ajax({		
		url:	"DeletePackageAjax?set_ajax_view",
		data: 	"package_id=" + packageId,				
		success: function(data) {
			$('tbody tr#' + packageId, $('#package_list')).nextAll().each(function () {
				var span = $(this).find('span.order');
				var order = span.text() - 1;				
				$(this).find('span.order').text(order);
				$(this).toggleClass('alt');
			});
		
			$('tbody tr#' + packageId, $('#package_list')).hide('slow').remove();
			if($('table#package_list tbody tr').length == 0){
				$('table#package_list tbody').append("<tr><td colspan='4' id='noresult'>" + _("No service package available") + "</td></tr>");
			}	
		} 
	});
}

////////////////////////// NEXT TO STEP 2 /////////////////
//
function next2Step2(nextStep) {		
	if ($.trim($('#package_name').val()).length  == 0) {		
		generalDialog(null,null,_("Empty service package name"),'warn');		
		return;
	}
	
	$.ajax({
		type:	"POST",
		url:	"Next2Step2Ajax?set_ajax_view",
		data: 	"package_id=" + $('#package_id').val() +
				"&name=" + $('#package_name').val() +
				"&note=" + $('#package_note').val(),
		dataType: "json",
		beforeSend: function() {
			$('#step1').hide('slow');
		},		
		success: function(data) {
			$('#package_id').val(data.package_id);
			$('#static_package_name').text(data.name);
			$('#static_package_note').text(data.note);
			
			if (nextStep) {
				listAvailableService();
				$('#step2').show('fast');
			} else {
				loadPackagePage('pageContent', 'ListPackage', '');
			}						
		} 
	});
}
/////////////////// LIST CURRENT SERVICES OF A PACKAGE //////////////
//
function listAvailableService() {
	$.ajax({
		type:	"POST",
		url:	"ListAvalilableServiceAjax?set_ajax_view",
		data: 	"package_id=" + $('#package_id').val(),					
		success: function(data) {
			$('table#ticket_item tbody').html(data);				
			styleTicketItemTable($('table#ticket_item'));			
		} 
	});
	
}
/////////////////////////// ADD NEW SERVICE ////////////////
//
function addService() {
	$.ajax ({
		type: 		"GET",
		url:		"AddServiceAjax?set_ajax_view",
		data:		"package_id=" 		+ $('input#package_id').val() +
					"&service_room_id="	+ $('select#service_room').val(),					
		beforeSend:	function() {
			$('input#add_service').attr('disabled', 'disabled');
			$('div#loading').show('fast');
		},
		complete:	function() {
			$('input#add_service').removeAttr('disabled');
			$('div#loading').hide('slow')
		},
		success:	function(result) {												
			$('table#ticket_item tbody').append(result);				
			styleTicketItemTable($('table#ticket_item'));							
		}
	});
}

///////////////////// DELETE A PACKAGE ITEM /////////////////
//
function deletePackageItem(packageItemId) {
	$.ajax ({
			type:		"GET",
			url:		"DeletePackageItemAjax?set_ajax_view",
			data:		"package_item_id=" + packageItemId,
			beforeSend:	function() {				
				$('div#loading').show('fast');
			},
			complete:	function() {				
				$('div#loading').hide('fast');
			},
			success:	function(data) {				
				// change ItemOrder and CSS class
				$('tr#'+packageItemId).nextAll().each(function () {
					var span = $(this).find('span.order');
					var order = span.text() - 1;				
					$(this).find('span.order').text(order);
					$(this).toggleClass('alt');
				});
				
				$('tr#'+packageItemId).hide('slow').remove();										
			}
		});	
}


	/////////////// POP UP WINDOW TO SELECT TEST ITEMS /////////////	
	// 1. ajax get all test item of current test
	// 2. onSuccess, popup the window
	function popUpATest() {			
		var testId = $('select#test_service').val();
		$.ajax({		
			url:		"../Test/ListItemAjax?set_ajax_view",
			data:		"test_id=" + testId,
			beforeSend:	function() {
				$('#add_test').attr('disabled', 'disabled');
				$('div#loading').show('fast');
			},
			complete: function(){			
				$('#add_test').removeAttr('disabled');
				$('div#loading').hide('fast');
			},
			success: 	function(data) {					
				$('div#add_test_dialog').find('table#test_item_data').remove();
				$('div#add_test_dialog').find('input#test_id').remove();
				$('div#add_test_dialog').append(data);	
				var buttons = {};
				buttons[_('Add test')] = function() {
					addTest();
					$(this).dialog('close');		
				};
				generalDialog('add_test_dialog',null,null,null,{
					autoOpen: true,			
					bgiframe: true,
					resizable: true,		
					width: 700,
					height: 400,
					modal: true,
					buttons:buttons
				});
			}
		});		
	}	
	
	/////////////// ADD A TEST SERVICE /////////////
	// ajax add an test item
	function addTest() {
		var queryString = "";
		var inputArray = new Array;
		$('input.test_item_popup').each(function() {			
			if ($(this).is(":checked")) {			
				queryString += "&test_item[]=" + $(this).attr('value');
				inputArray.push($(this).attr('value'));
			}
		});
		
		if (inputArray.length == 0) {
			$('div#add_test_dialog').dialog('close');
			return false;
		}
		
		$.ajax ({				
			url: 	"AddTestAjax?set_ajax_view",
			data: 	"package_id=" +	$('input#package_id').val() + 
					"&test_id=" + $('input#test_id').val() + queryString,
			beforeSend:	function() {
				$('#add_test').attr('disabled', 'disabled');					
				$('div#loading').show('fast');
				$('div#add_test_dialog').dialog('close');
			},
			complete: function(){	
				$('#add_test').removeAttr('disabled');								
				$('div#loading').hide('fast');					
			},
			success: function(result) {									
				$('table#ticket_item tbody').append(result);
				styleTicketItemTable($('table#ticket_item'));									
			}
		});
	}

	/////////////// POP UP TEST DETAILS /////////////	
	// ajax show test details
	function testDetail(packageTestId) {	
		
		$.ajax({
			type:		"GET",
			url:		"TestDetailAjax?set_ajax_view",
			data: 		"package_test_id=" + packageTestId,
			success:	function(data) {
				$('div#test_detail_dialog').html(data);				
				generalDialog('test_detail_dialog',null,null,null,{
					autoOpen: true,			
					bgiframe: true,
					resizable: true,		
					width: 700,
					height: 550,
					modal: true
				});
			}
			
		});
	}

/////////////////////// FILTER SERVICE, ROOM BY DEPARTMENT //////////////////
//
// filter service by department
function ajaxServiceFilter(department) {
	$.ajax ({
		type:		"GET",
		url:		'../Department/FilterServiceAjax?set_ajax_view',
		data:		'department_id=' + department.val(),
		beforeSend:	function() {
			$('input#add_service').attr('disabled', 'disabled');
		},		
		success: function(data) {
			$('select#service').html(data);
			ajaxRoomFilter($('select#service'));		
		}
	});	
}

// filter room by service
function ajaxRoomFilter(service) {
	$.ajax ({
		type:		"GET",
		url:		'../Service/FilterRoomAjax?set_ajax_view',
		data:		'service_id=' + service.val(),
		beforeSend:	function() {
			$('input#add_service').attr('disabled', 'disabled');
		},
		complete: function(){			
			$('input#add_service').removeAttr('disabled');
		},
		success: function(data) {
			$('select#service_room').html(data);
		}
	});			
}


function styleTicketItemTable(table) {
								
}


